package cn.tedu.mvc.controller;


import cn.tedu.mvc.entity.User;
import cn.tedu.mvc.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.security.Principal;

@RestController
@RequestMapping("/users")
public class UserController {

    //@Autowired
    UserService userService;

    public UserController(UserService userService){
        System.out.println("UserController.UserController");
        this.userService = userService;
    }

    @PostMapping("/regist")
    public String regist(String username, String password){
        userService.regist(username,password);
        return "注册成功！";
    }

    @GetMapping("/login")
    public String login(String username,String password){
        System.out.println(username);
        System.out.println(password);
        User user = userService.login(username,password);
        return "登录成功!";
    }

    // /users?user-name=tom
    @GetMapping()
    public User get(@RequestParam("user-name") String username,
                    HttpSession session,
                    HttpServletRequest request,
                    HttpServletResponse response,
                    Principal principal){
        System.out.println(session);
        System.out.println(request);
        System.out.println(response);
        System.out.println(principal); //当前登录用户
        return userService.findUser(username);
    }

    @GetMapping("/{id}")
    public User get(@PathVariable("id") Integer userid,
                    ModelMap modelMap){
        //利用modelMap(就是特殊的 model对象) 向视图View传递数据
        modelMap.addAttribute("message", "成功");
        return userService.findUser(userid);
    }
}
